﻿using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

namespace ExampleService.APIContract
{
    /// <summary>
    /// 服务契约层 的 扩展 [用于内部服务间的调用]   
    /// </summary>
    public static class ExampleServiceExtensions
    {
        /// <summary>
        /// 注册契约接口
        /// 命名 与 服务名 保持一致 方便区分
        /// </summary>
        /// <param name="services"></param>
        /// <param name="baseUri"></param>
        public static void AddExampleService(this IServiceCollection services, string baseUri = "http://localhost:5208")
        {
            var httpApiTypes = Assembly.GetExecutingAssembly().GetTypes()
                .Where(type => type.IsInterface && type.IsPublic && type.IsDefined(typeof(ContractAttribute), false));

            foreach (var httpApiType in httpApiTypes)
            {
                var method = typeof(HttpApiExtensions).GetMethod("AddHttpApi", new[] { typeof(IServiceCollection), typeof(Type), typeof(string) });
                method?.Invoke(null, new object[] { services, httpApiType, baseUri });
            }
        }




    }
}
